package com.example.memoriesfall.handler;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
    private Button Start = null;
    private Button End = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setContentView(R.layout.activity_main);
        Start = (Button)findViewById(R.id.start);
        Start.setOnClickListener(new StartListener());
        End = (Button)findViewById(R.id.end);
        End.setOnClickListener(new EndListener());

    }
    Handler handler = new Handler();
    class StartListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            handler.post(updateThread);
        }
    }
    class EndListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            handler.removeCallbacks(updateThread);
        }
    }
    Runnable updateThread = new Runnable() {
        @Override
        public void run() {
            System.out.println("UpdateThread");
            Log.v("tag", "UpdateThread");
            //在run方法内部，执行postDelayed或者是post方法
            handler.postDelayed(updateThread, 2000);
        }
    };
}
